program rewrite_afglms

    implicit none
    character(len=128) :: cfile = &
        "/home/andreyj/libRadtran/share/libRadtran/data/atmmod/afglmw.dat"

    integer, parameter :: nprof = 9, nlevl = 50
    real(4), dimension(9,50) :: datos
    integer :: i

    open(1,file=cfile, status='old', action='read')
    read(1,*)
    read(1,*)
    read(1,*) datos
    close(1)
    
    do i = 5,9
        datos(i,:) = datos(i,:) / datos(4,:) * 1D6
    enddo

    write(100,'(4X,"real(kind=JPR4), parameter  :: AFGLMS_P(NLAFG)  = (/",2(F10.5,", "),"&")') datos(2,1:2)
    write(100,'(10(8X,6(F10.5,", "),"&",/))') datos(2,3:)
    
    write(100,*)
    write(100,'(4X,"real(kind=JPR4), parameter  :: AFGLMS_AIR(NLAFG)= (/",2(ES12.6,", "),"&")') datos(4,1:2)
    write(100,'(10(8X,6(ES12.6,", "),"&",/))') datos(4,3:)

    write(100,*)
    write(100,'(4X,"real(kind=JPR4), parameter  :: AFGLMS_T(NLAFG)  = (/",2(F8.2,", "),"&")') datos(3,1:2)
    write(100,'(10(8X,6(F8.2,", "),"&",/))') datos(3,3:)

    write(100,*)
    write(100,'(4X,"real(kind=JPR4), parameter  :: AFGLMS_O3(NLAFG)  = (/",2(ES12.6,", "),"&")') datos(5,1:2)
    write(100,'(10(8X,6(ES12.6,", "),"&",/))') datos(5,3:)

    write(100,*)
    write(100,'(4X,"real(kind=JPR4), parameter  :: AFGLMS_O2(NLAFG)  = (/",2(ES12.6,", "),"&")') datos(6,1:2)
    write(100,'(10(8X,6(ES12.6,", "),"&",/))') datos(6,3:)
    write(100,*)
    
    write(100,'(4X,"real(kind=JPR4), parameter  :: AFGLMS_H2O(NLAFG)  = (/",2(ES12.6,", "),"&")') datos(7,1:2)
    write(100,'(10(8X,6(ES12.6,", "),"&",/))') datos(7,3:)
    write(100,*)
    
    write(100,'(4X,"real(kind=JPR4), parameter  :: AFGLMS_CO2(NLAFG)  = (/",2(ES12.6,", "),"&")') datos(8,1:2)
    write(100,'(10(8X,6(ES12.6,", "),"&",/))') datos(8,3:)
    write(100,*)
    
    write(100,'(4X,"real(kind=JPR4), parameter  :: AFGLMS_NO2(NLAFG)  = (/",2(ES12.6,", "),"&")') datos(9,1:2)
    write(100,'(10(8X,6(ES12.6,", "),"&",/))') datos(9,3:)

end program rewrite_afglms
